package com.example.testone

import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import com.example.testone.databinding.ActivityMainCopyBinding
import com.example.testone.utils.ByteUtil
import com.example.testone.utils.MySerialPort
import com.example.testone.utils.SerialPortUtils
import com.example.testone.utils.UsbPermission
import com.hoho.android.usbserial.driver.UsbSerialPort
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject

@AndroidEntryPoint
class MainActivityCopy : AppCompatActivity() {


    lateinit var binding: ActivityMainCopyBinding
    @Inject
    lateinit var serialPortUtil: MySerialPort

    var number = 10
    override fun onCreate(savedInstanceState: Bundle?) {



        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this,R.layout.activity_main_copy)
        setContentView(binding.root)


        binding.conn.setOnClickListener {
            serialPortUtil.resetConn()
        }


        //60 61 00 00 00 C1
        //开机--20 21 1E 3C 00 9B
        //关机--20 21 1E 3C 00 9B
        binding.open.setOnClickListener {
//            serialPortUtil.open()
            val buffer = byteArrayOf(0x20,0x21,0x1E,0x3C,0x00, 0x9B.toByte())
            Log.e("hehe", "open Read $ bytes.${serialPortUtil.serialPort.isOpen}")
            serialPortUtil.serialPort.sendBytes(buffer)
        }
        binding.close.setOnClickListener {
            val buffer = byteArrayOf(0x20,0x29,0x00,0x00,0x00, 0x42.toByte())
            serialPortUtil.serialPort.sendBytes(buffer)
            Log.e("hehe", "close Read $ bytes.${serialPortUtil.serialPort.isOpen}")
        }
        binding.lightplus.setOnClickListener {
            if(number<10){
                number++
            }
            val byteData = byteArrayOf(0x20,0x2E,number.toByte(),0x00,0x00)

            val buffer = byteData+ByteUtil.sign(byteData);//byteArrayOf(0x20,0x2E,0x0A,0x00,0x00, 0x58.toByte())
            serialPortUtil.serialPort.sendBytes(buffer)
            Log.e("hehe", "lightplus Read $ bytes.${serialPortUtil.serialPort.isOpen};number:${number};buffer:${ByteUtil.toHexStr(buffer)}")
        }
        binding.lightSub.setOnClickListener {
            if(number>0){
                number--
            }
            val byteData = byteArrayOf(0x20,0x2E,number.toByte(),0x00,0x00)
            val buffer = byteData+ByteUtil.sign(byteData);//byteArrayOf(0x20,0x2E,0x0A,0x00,0x00, 0x58.toByte())
            serialPortUtil.serialPort.sendBytes(buffer)
            Log.e("hehe", "lightSub Read $ bytes.${serialPortUtil.serialPort.isOpen};number:${number};buffer:${ByteUtil.toHexStr(buffer)}")
        }
        binding.work.setOnClickListener {

            val byteData = byteArrayOf(0x20,0x28,number.toByte(),0x00,0x00)
            val buffer = byteData+ByteUtil.sign(byteData);//byteArrayOf(0x20,0x2E,0x0A,0x00,0x00, 0x58.toByte())
            serialPortUtil.serialPort.sendBytes(buffer)
            Log.e("hehe", "work Read $ bytes.${serialPortUtil.serialPort.isOpen};number:${number};buffer:${ByteUtil.toHexStr(buffer)}")
        }

        binding.end.setOnClickListener {
            val byteData = byteArrayOf(0x20,0x29,number.toByte(),0x00,0x00)
            val buffer = byteData+ByteUtil.sign(byteData);//byteArrayOf(0x20,0x2E,0x0A,0x00,0x00, 0x58.toByte())
            serialPortUtil.serialPort.sendBytes(buffer)
            Log.e("hehe", "end Read $ bytes.${serialPortUtil.serialPort.isOpen};number:${number};buffer:${ByteUtil.toHexStr(buffer)}")
        }


        binding.xintiao.setOnClickListener {
            val byteData = byteArrayOf(0x80.toByte(),0x89.toByte(),0x00,0x00,0x00)
            val buffer = byteData+ByteUtil.sign(byteData);//byteArrayOf(0x20,0x2E,0x0A,0x00,0x00, 0x58.toByte())
            serialPortUtil.serialPort.sendBytes(buffer)
            Log.e("hehe", "xintiao Read $ bytes.${serialPortUtil.serialPort.isOpen};number:${number};buffer:${ByteUtil.toHexStr(buffer)}")
        }

    }
}

